package remote.mvc.controls
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	import remote.mvc.commands.ICommand;
	
	
	/**
	 * 调度器，获取数据时使用
	 * @createDate 2011-2-28
	 * @author 徐俊杰
	 */
	public class Dispatcher extends EventDispatcher
	{
		private static var _instance:Dispatcher;
		
		/**
		 *  调度器单例
		 * 
		 */		
		public static function get instance():Dispatcher
		{
			if(_instance == null)
				_instance = new Dispatcher();
			return _instance;
		}
		
		
		private var _commands:Dictionary;
		public function Dispatcher()
		{
			super();
			_commands = new Dictionary();
		}
		
		override public function dispatchEvent(event:Event):Boolean
		{
			var result:Boolean = super.dispatchEvent(event);
			if(_commands[event.type] != null)
			{
				((new (_commands[event.type] as Class)) as ICommand).execute(event);
			}
			return result;
		}
		
		/**
		 * 添加事件处理 
		 * @param type 事件类型
		 * @param command 处理类
		 * 
		 */		
		public function addCommand(type:String, command:Class):void
		{
			_commands[type] = command;
		}
		
		/**
		 * 移除事件处理 
		 * @param type 事件类型
		 * 
		 */		
		public function removeCommand(type:String):void
		{
			delete _commands[type];
		}
		
		/**
		 * 获取某类型事件的处理类 
		 * @param type 事件类型
		 * @return 处理类
		 * 
		 */		
		public function getResponseClass(type:String):Class
		{
			return _commands[type] as Class;
		}
	}
}